<template>
  <div>
    <el-form ref="form" :rules="rules" :model="fifter" label-width="80px">
      <el-form-item label="原始密码" prop="password">
        <el-input v-model="fifter.password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password1">
        <el-input v-model="fifter.password1" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="password2">
        <el-input v-model="fifter.password2" show-password></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resetForm('formRef')">取 消</el-button>
      <el-button type="primary" @click="xgmmqr('form')">确 定</el-button>
    </span>
  </div>
</template>
<script>
export default {
  name: "",
  data() {
    return {
      fifter: {
        password: "",
        password1: "",
        password2: ""
      },
      rules: {
        password: [
          { required: true, message: "请填写旧密码", trigger: "blur" }
        ],
        password1: [
          { required: true, message: "请填写新密码", trigger: "blur" }
        ],
        password2: [
          { required: true, message: "请填写确认密码", trigger: "blur" }
        ]
      }
    };
  },
  components: {},
  computed: {},
  beforeMount() {},
  mounted() {},
  methods: {
    resetForm(formName) {
      this.$emit("submit", false);
      this.$refs[formName].resetFields();
    },
    xgmmqr(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.fifter.password2 !== this.fifter.password1) {
            this.$message.error("确认密码与新密码不同");
          } else {
            let param = {
              username: this.Global.user.username,
              oldPassword: this.fifter.password,
              newPassword: this.fifter.password1
            };
            this.$service
              .modifyPassWord(
                param.oldPassword,
                param.newPassword,
                param.username
              )
              .then(res => {
                if (res.code == "200") {
                  this.$message({
                    type: "success",
                    message: "修改密码成功"
                  });
                  this.$emit("submit");
                  this.$refs[formName].resetFields();
                  localStorage.setItem("isLogin", null);
                  this.$router.replace("/");
                  // localStorage.removeItem("currentUser");
                  localStorage.removeItem("currentUser");
                } else {
                  this.$message({
                    type: "error",
                    message: res.message
                  });
                }
              });
          }
        }
      });
    }
  },
  watch: {}
};
</script>
<style scoped>
</style>
